package com.jhd.fuzhou.view.special;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.util.CustomToast;
import com.jhd.fuzhou.util.EditTextView;
import com.jhd.fuzhou.vo.CheckValue;
import com.jhd.fuzhou.vo.DetectedItemsVo;
import com.jhd.fuzhou.vo.GlobalDictionaryVo;

import net.zigin.commons.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jhd.fuzhou.view.login.MainActivity.supervisionInspectionVo;

public class SpecialLaboratoryCheckAddActivity extends BaseActivity {
    private Context context= this;
    private AlertDialog _alertDialog=null;
    private String checkValue;
    private List<GlobalDictionaryVo> globalDictionaryVoList;
    @BindView(R.id.btn_confirm)
    Button btn_confirm;
    @BindView(R.id.et_sample_number)
    EditText et_sample_number;
    @BindView(R.id.et_qualified_number)EditText et_qualified_number;
    @BindView(R.id.tv_check_name)
    TextView tv_check_name;
    @BindView(R.id.rel_check_name)
    RelativeLayout rel_check_name;
    private List<DetectedItemsVo> detectedItemsVoList;
    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_laboratory_check_add);
    }

    @Override
    protected void processLogic() {
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        checkValue=getIntent().getStringExtra("checkValue");
        if(StringUtil.isNullOrEmpty(checkValue)==false) {
            globalDictionaryVoList = JSON.parseObject(checkValue, CheckValue.class).getGlobalDictionaryVoMap();
        }
    }
    @OnClick({R.id.btn_confirm,R.id.rel_check_name})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_confirm://先要将数据加入检测记录，再关闭
                String checkName=tv_check_name.getText().toString().trim();
                String sampleNumber=et_sample_number.getText().toString().trim();
                String qualifiedNumber=et_qualified_number.getText().toString().trim();
                if(checkName.equals("请选择")){
                    CustomToast.showText(context,"请先选择检测项目!",3000,0,0).show();
                }else {
                    if (sampleNumber.equals("") || qualifiedNumber.equals("")) {
                        CustomToast.showText(context,"样品数量和合格数量不能为空!",3000,0,0).show();
                    } else {
                        if (Integer.valueOf(qualifiedNumber) > Integer.valueOf(sampleNumber)) {
                            CustomToast.showText(context,"合格数量不能大于样品数量!",3000,0,0).show();
                        } else {
                            if (supervisionInspectionVo.getDetectedItemsVoList() != null) {
                                detectedItemsVoList = supervisionInspectionVo.getDetectedItemsVoList();
                            } else {
                                detectedItemsVoList = new ArrayList<>();
                            }
                            DetectedItemsVo detectedItemsVo = new DetectedItemsVo();
                            detectedItemsVo.setCheckType("1");
                            detectedItemsVo.setStatus("1");
                            detectedItemsVo.setSampleQuantity(Integer.valueOf(sampleNumber));
                            detectedItemsVo.setQualifiedQuantity(Integer.valueOf(qualifiedNumber));
                            //detectedItemsVo.setSort(num);最后再加
                            detectedItemsVo.setItemName(checkName);
                            detectedItemsVoList.add(detectedItemsVo);
                            supervisionInspectionVo.setDetectedItemsVoList(detectedItemsVoList);
                            finish();//点击完成，关闭返回显示页面
                        }
                    }
                }
                break;
            case R.id.rel_check_name:
                dialogShow("检测项目",tv_check_name,globalDictionaryVoList);
                break;
        }
    }

    public void dialogShow(String title,TextView textView, List<GlobalDictionaryVo> globalDictionaryVoList) {
        LayoutInflater mLayoutInflater = LayoutInflater.from(this);
        View view = mLayoutInflater.inflate(R.layout.dialog_choose, null);
        final androidx.appcompat.app.AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context, R.style.ActionSheetDialogStyle);
        alertBuilder.setView(view);
        _alertDialog = alertBuilder.create();
        //获取当前Activity所在的窗体
        Window window = _alertDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        _alertDialog.show();
        ImageView iv_dismiss = (ImageView) view.findViewById(R.id.iv_dismiss);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        RecyclerView rv_choose = (RecyclerView) view.findViewById(R.id.rv_choose);
        LinearLayoutManager layoutManage = new LinearLayoutManager(context);
        rv_choose.setLayoutManager(layoutManage);
        tv_title.setText(title);//标题要改一下
        CheckAdapter recyclerAdapter=new CheckAdapter(textView,_alertDialog,globalDictionaryVoList);
        rv_choose.setAdapter(recyclerAdapter);
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _alertDialog.dismiss();
            }
        });
        WindowManager.LayoutParams params = window.getAttributes() ;
        params.width =(int) (getWindowManager().getDefaultDisplay().getWidth());                     //使用这种方式更改了dialog的框宽
        window.setAttributes(params);
    }
    public class CheckAdapter extends RecyclerView.Adapter<CheckAdapter.ViewHolder> {
        private List<GlobalDictionaryVo> globalDictionaryVoList;
        private TextView textView;
        private AlertDialog _alertDialog;
        private int checkPosition=-1;

        public CheckAdapter(TextView textView, AlertDialog _alertDialog, List<GlobalDictionaryVo> globalDictionaryVoList) {
            this.globalDictionaryVoList = globalDictionaryVoList;
            this._alertDialog = _alertDialog;
            this.textView = textView;
        }

        @NonNull
        @Override
        public CheckAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question_choose, parent, false);
            CheckAdapter.ViewHolder viewHolder = new CheckAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull CheckAdapter.ViewHolder holder, int position) {
            holder.tv_choose.setText(globalDictionaryVoList.get(position).getKey());
            if(globalDictionaryVoList.get(position).getKey().equals(textView.getText().toString())){
                holder.image.setVisibility(View.VISIBLE);
            }else{
                holder.image.setVisibility(View.GONE);
            }

            holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    textView.setText(globalDictionaryVoList.get(position).getKey());
                    EditTextView.TextViewChange(textView,context);
                    holder.image.setVisibility(View.VISIBLE);
                    _alertDialog.dismiss();
                    checkPosition=position;
                    notifyDataSetChanged();
                }
            });

            if(position==checkPosition){
                holder.image.setVisibility(View.VISIBLE);
            }else{
                if(checkPosition!=-1){
                    holder.image.setVisibility(View.GONE);
                }
            }
        }

        @Override
        public int getItemCount() {
            return globalDictionaryVoList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_choose;ImageView image;
            RelativeLayout relativeLayout;
            public ViewHolder(View itemView) {
                super(itemView);
                this.tv_choose = itemView.findViewById(R.id.tv_choose);
                this.image = itemView.findViewById(R.id.image);
                this.relativeLayout = itemView.findViewById(R.id.relativeLayout);
            }
        }
    }
}